package com.design.graduation.asdemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.design.graduation.asdemo.R;
import com.design.graduation.asdemo.model.InfoEntity;
import com.design.graduation.asdemo.utils.TipsDialog;

import java.util.List;

/**
 * Created by lijinyuan on 16/3/1.
 */
public class LocationActivity extends Activity implements View.OnClickListener {
    protected AMap aMap;
    protected MapView mapView;
    private TipsDialog tipsDialog;
    protected List<InfoEntity> infoEntities;
    protected float currentScale = 15f;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        mapView = (MapView) findViewById(R.id.aMap);
        mapView.onCreate(savedInstanceState);

        if (aMap == null) {
            aMap = mapView.getMap();
        }

        setUpMap();
        findViewById(R.id.my_location).setOnClickListener(this);

    }


    private void setUpMap() {
        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setScaleControlsEnabled(true);
        uiSettings.setMyLocationButtonEnabled(false);
        uiSettings.setZoomControlsEnabled(false);
        uiSettings.setRotateGesturesEnabled(false);
        aMap.setMyLocationEnabled(true);
        aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null && outState != null) {
            mapView.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }
//
//    private void initLastRecordLocation() {
//        if (infoEntities != null && infoEntities.size() > 0) {
//            InfoEntity entity = infoEntities.get(infoEntities.size() - 1);
//            animateCamera(entity.getLon(), entity.getLat());
//        } else {
//            cameraLastLocation();
//        }
//    }
//
//    private void cameraLastLocation() {
//        InfoEntity last = null;
//        try {
//            last = SharePreferenceUtils.getLastLocation();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (last != null) {
//            animateCamera(last.getLon(), last.getLat());
//        } else {
//            last = new InfoEntity(104.070927, 30.651571, 0);
//            animateCamera(last.getLon(), last.getLat(), 10);
//        }
//    }
//
//    private void animateCamera(double lon, double lat) {
//        if (aMap != null) {
//            aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
//                    new LatLng(lat, lon), currentScale));
//        }
//    }
//
//    private void animateCamera(double lon, double lat, float scale) {
//        if (aMap != null) {
//            aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
//                    new LatLng(lat, lon), scale));
//        }
//    }

    @Override
    public void onClick(View v) {
        if (tipsDialog == null) {
            tipsDialog = new TipsDialog(LocationActivity.this, new TipsDialog.EnsureOrCancelListener() {
                @Override
                public void onEnsureClicked() {
                    LatLng latLng = aMap.getCameraPosition().target;
                    InfoEntity temp = new InfoEntity(latLng.longitude, latLng.latitude, 0);
//                    Intent data = new Intent();
//                    data.putExtra(Constant.LOCATION, temp);
//                    setResult(RESULT_OK, data);
//                    finish();
                    Log.d("ssssssssss", temp.getLon()+"zheshi");

                }

                @Override
                public void onCancelClicked() {
                }
            }, "是否要修改为当前位置?");
        }
        tipsDialog.show();
    }
}
